﻿using System;
using System.Collections;
using System.Linq;
using System.Text;
using SimioAPI;
using SimioAPI.Extensions;
using System.Windows.Forms;

namespace WLC
{
    class WLCBOMElementDefinition : IElementDefinition
    {
        #region IElementDefinition Members

        public IElement CreateElement(IElementData data)
        {
            return new WLCBOMElement(data);
        }


        public void DefineSchema(IElementSchema schema)
        {
            IPropertyDefinitions propSchema = schema.PropertyDefinitions;
            IPropertyDefinitions repProp = propSchema.AddRepeatGroup("WLCBillOfMaterial", "The repeated property group describing the bill of material (first level only).");
            
            IPropertyDefinition itemName = repProp.AddStringProperty("ItemName", String.Empty);
            itemName.Description = "The name of the item that is needed for the production of this item.";
            itemName.Required = true;

            IPropertyDefinition amount = repProp.AddExpressionProperty("Amount", String.Empty);
            amount.Description = "The amount required of this item.";
            amount.Required = true;

            IPropertyDefinition creatingSourceState = repProp.AddStateProperty("CreatingSourceState");
            creatingSourceState.Description = "The source's state variable to set in order to create a new production order.";
            creatingSourceState.Required = true;

            IPropertyDefinition creatingSourceSizeState = repProp.AddStateProperty("CreatingSourceSizeState");
            creatingSourceSizeState.Description = "The source's state variable to temporarily set the size of the next created order.";
            creatingSourceSizeState.Required = true;

            IPropertyDefinition creatingSourceDueDate = repProp.AddStateProperty("CreatingSourceDueDate");
            creatingSourceDueDate.Description = "The source's state variable to temporarily set the due date of the next created order.";
            creatingSourceDueDate.Required = true;

           
            
            
        }

        public string Name
        {
            get { return "WLCBOM"; }
        }

        public string Description
        {
            get { return ""; }
        }

        public System.Drawing.Image Icon
        {
            get { return null; }
        }

              
        
        public static readonly Guid ID = new Guid("{11BD1D74-1996-11DF-B138-B41F56D89593}");

        public Guid UniqueID
        {
            get { return ID; }
        }

        #endregion
    }


    public class WLCBOMElement : IElement
    {
        IElementData _data;
        IRepeatingPropertyReader _wlcBillOfMaterialProp;
        public WLCBOMElement(IElementData data)
        {
            _data = data;
            _wlcBillOfMaterialProp = (IRepeatingPropertyReader)data.Properties.GetProperty("WLCBillOfMaterial");

        }

        #region IElement Members


        public Hashtable getBOMEntries()
        {
            Hashtable entries = new Hashtable();

            IPropertyReaders currentRow;

            BOMEntry entry;

            for(int i=0; i<_wlcBillOfMaterialProp.GetCount(_data.ExecutionContext); i++) {

                currentRow = _wlcBillOfMaterialProp.GetRow(i, _data.ExecutionContext);
                entry = new BOMEntry((double)currentRow.GetProperty("Amount").GetDoubleValue(_data.ExecutionContext), ((IStateProperty)currentRow.GetProperty("CreatingSourceState")).GetState(_data.ExecutionContext), ((IStateProperty)currentRow.GetProperty("CreatingSourceSizeState")).GetState(_data.ExecutionContext), ((IStateProperty)currentRow.GetProperty("CreatingSourceDueDate")).GetState(_data.ExecutionContext));
                entries.Add(currentRow.GetProperty("ItemName").GetStringValue(_data.ExecutionContext) , entry);
            
            }
            return entries;
        }

        public void Initialize()
        {

           
           
        }

        public void Shutdown()
        {
            // No shutdown code necessary
        }

        

        #endregion
    }

     class BOMEntry
    {
        public double Amount { get; set; }
        public IState CreatingSourceState { get; set; }
        public IState CreatingSourceSizeState { get; set; }
        public IState CreatingSourceDueDateState { get; set; }

        public BOMEntry(double amount, IState state, IState size, IState duedate)
        {
            this.Amount = amount;
            this.CreatingSourceState = state;
            this.CreatingSourceSizeState = size;
            this.CreatingSourceDueDateState = duedate;
        }

    }



}

